package com.example.htmlfx;

import com.example.htmlfx.view.TableFxmlView;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import de.felixroske.jfxsupport.SplashScreen;
import javafx.concurrent.Task;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HtmlfxApplication extends AbstractJavaFxApplicationSupport {

  @Override
  public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/fxml/table.fxml"));
    Scene scene = new Scene(root);
    stage
        .getIcons()
        .add(
            new Image(
                HtmlfxApplication.class.getResource("/images/yzj_loading.gif").toExternalForm()));
    stage.setResizable(false);
    stage.setTitle("页面解析器 - gzkemays");
    stage.setScene(scene);
    Stage splashStage = new Stage(StageStyle.TRANSPARENT);
    SplashScreen splashScreen = new CustomerSplashScreen();
    if (splashScreen.visible()) {
      Scene splashScene = new Scene(splashScreen.getParent(), Color.TRANSPARENT);
      splashStage.setScene(splashScene);
      splashStage.initStyle(StageStyle.TRANSPARENT);
      this.beforeShowingSplash(splashStage);
      splashStage.show();
    }
    // 模拟同步加载页面，实则 sleep 3 秒
    ProgressIndicator progressIndicator = new ProgressIndicator();
    Task<Boolean> sleepTask =
        new Task<Boolean>() {
          @Override
          protected Boolean call() throws Exception {
            Thread.sleep(3000);
            return true;
          }
        };
    progressIndicator.progressProperty().bind(sleepTask.progressProperty());
    Thread sleepThread = new Thread(sleepTask);
    sleepThread.start();
    sleepTask.setOnSucceeded(
        event -> {
          splashStage.hide();
          splashStage.setScene((Scene) null);
          stage.show();
        });
  }

  public static void main(String[] args) {
    launch(HtmlfxApplication.class, TableFxmlView.class, new CustomerSplashScreen(), args);
  }
}
